package com.meiyou.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meiyou.pojo.Chongzhi;
import com.meiyou.pojo.User;
import com.meiyou.service.ChongzhiService;
import com.meiyou.utils.Msg;
import com.meiyou.utils.PagesMap;
import com.meiyou.utils.StringId;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "充值接口", tags = { "充值接口" })
@RequestMapping("/chongzhi")
public class ChongzhiController {

	@Autowired
	ChongzhiService chongzhiService;
	
	//添加充值记录
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="充值记录",notes="充值记录",httpMethod="POST")
	public Msg add(String username,String cPaytype,double cMeijin,int statu) {
		//statu属性  1 表示充值成功  2 表示取消充值
		//1.查询出没充值前金币
		double qmeijin = chongzhiService.selectMeijin(username);
		if(statu==1) {
			//充值成功
			//2.添加美金
			User user = new User();
			double xmeijin = cMeijin+qmeijin;
			user.setuMeijin(xmeijin);
			user.setuPhone(username); 
			chongzhiService.updateMeijin(user);
			//3.查询充值之后美金
			double hmeiji = chongzhiService.selectMeijin(username);
			//4.保存充值记录
			Chongzhi chongzhi = new Chongzhi();
			chongzhi.setcUCount(username);
			chongzhi.setcPaytype(cPaytype);
			chongzhi.setcUMeijing(hmeiji);
			chongzhi.setcMeijin(cMeijin);
			chongzhi.setcState("充值成功");
			Date date = new Date();
			chongzhi.setcTime(date);
			String id = StringId.genItemId()+"";
			chongzhi.setcCzode(id); 
			chongzhiService.addChongzhijilu(chongzhi);
		}
		if(statu==2) {
			//取消充值
			//2.保存充值记录
			Chongzhi chongzhi = new Chongzhi();
			String id = StringId.genItemId()+"";
			chongzhi.setcUCount(username);
			chongzhi.setcPaytype(cPaytype);
			chongzhi.setcUMeijing(qmeijin);
			chongzhi.setcMeijin(cMeijin);
			chongzhi.setcState("取消充值");
			chongzhi.setcCzode(id);
			Date date = new Date();
			chongzhi.setcTime(date);
			chongzhiService.addChongzhijilu(chongzhi);
		}
		return Msg.success(); 
	}

	//查询充值记录
	@RequestMapping(value="/select",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="查询充值记录",notes="查询充值记录",httpMethod="POST")
	public Msg select(int pn,String strtime) {
		
		Chongzhi chongzhi = new Chongzhi();
		if(strtime!=null &&strtime!="") {
			String s[] = strtime.split(",");
			chongzhi.setStarttime(s[0]);
			chongzhi.setEndtime(s[1]);
		}
		List<Chongzhi> list = chongzhiService.selectChongzhijilu(chongzhi);	
		Map<String,Object> map = PagesMap.getpagesChongzhiMaps(list, pn);
		return Msg.success().add("pageInfo", map);  
	}
	
	//删除充值记录
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="删除充值记录",notes="删除充值记录",httpMethod="POST")
	public Msg delete(int id) {
		
		chongzhiService.delete(id);
		return Msg.success();  
	}
}
